var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var wx_location = require('modules/weixin/getLocation')

var api = libs.api;
var valide = libs.formValide;
var _ = libs.lodash;

//当前页公共变量
var _page = {
    sites: false
}

var bindEvent = function(){
    var _select;
    //此处各种dom 操作
    $(tihs).find('li').click(function(){
        // alert($(this).attr('data-id'))
        var did = $(this).attr('data-id')
        if (_.isArray(_page.sites)){
            _select = _.find(_page.sites, {communityId: did})
            /*
                异步操作并router到下一步
                router('xxx')
            */
        }
    })

};

//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    getInitialState:function(){
		return {
            sites: []
		}
	},
    componentWillMount:function(){
        if (this.props.data){
            //     [
            //       {
            //         "cityId": "440100",
            //         "cityName": "440100",
            //         "communityId": "6",
            //         "communityName": "美林湖畔",
            //         "distances": "20",//单位米
            //         "districtId": "440106",
            //         "districtName": "天河区",
            //         "provinceId": "440000",
            //         "provinceName": "广东省"
            //       },
            //       ...
            //   ]

            var _data = this.props.data;
            var _sites = []
            if (libs.type(_data)==='Array'){
                _data.map(function(item){
                    _sites.push(
                        <li data-id={item.communityId}>
                            <label>{item.communityName}</label>
                            <i className="iconfont icon-xiangyou"></i>
                        </li>
                    )
                })
                this.setState({
                    sites: _sites
                })
            }
        }
    },
    render:function(){
        return (
            <div className="autolocation">
                <h3>定位成功，选择您的小区</h3>
                <ul className="default_Picture">
                    {/*<li>
                        <label>小区1</label>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    ...
                    */}
                    {this.state.sites}
                </ul>
            </div>
        );
    }
});

//传入经纬度获取小区数组
function getZone(lat, lng){
    var me = this;
    var _login = this.preData.login;
    var postdata = {
        longitude: lng,
        latitude: lat,
        login: _login,
        method: 'get'
    }
    //请求小区点
    api.req('/locationCommunitys', postdata, function(data){
        //错误数据
        //跳转到手动选择
        if (data.code){
            libs.msgtips('没有匹配到您附近的服务点，请手动选择')
            setTimeout(function(){
                router('uc/location')
            }, 3000)
        }
        //正确数据
        //渲染结构
        else {
            _page.sites = data
            me.main(me, data)
        }
    })
}

// 获取经纬度
// this为page的实例
function getLocation(){
    var me = this;
    if (wx){
        setTimeout(function(){
            router('uc/location')
        },1000)

        // wx_location(
        //     //success
        //     function(lat,lng){
        //         getZone.call(me, lat, lng)
        //     },
        //
        //     //error
        //     function(){
        //         libs.msgtips('没有匹配到您附近的服务点，请手动选择')
        //         setTimeout(function(){
        //             router('uc/location')
        //         }, 3000)
        //     }
        // );
    }
    else{
        navigator.geolocation.getCurrentPosition(function(position){
            getZone.call(me, coords.latitude, coords.longitude)
        })
    }
}

function start(name){
    return pages.new({
        boot:function(){

        },
        ready: function(){

        },
        trigger:function(self, preData){
            this.libs.changeTitle('自动定位');    //更改当前页面标题
            this.preData = preData;
            getLocation.call(this)
        },
        main: function(self, data){
            this.render(
                <Show itemDefaultMethod={bindEvent} data={data} />,
                document.getElementById(name)
            )
        }
    })
}

module.exports = start;
